home *** CD-ROM | disk | FTP | other *** search
/ PsL Monthly 1994 December / PSL Monthly Shareware CD-ROM (Public Software Library)(December 1994).bin / prgmming / win / pascal / strparse.pas < prev    next >
Pascal/Delphi Source File  |  1992-03-16  |  4KB  |  136 lines

  1. UNIT STRPARSE;
  2.  
  3. {
  4.         This is a Pascal Version of the 'C' Functions StrTok, Which is a
  5.  String Parsing Routine. I wrote this routine because I'm basicly a 'C'
  6.  programmer but had to write an application in Pascal. I'm uploading this
  7.  routine to other because I think they'll find it useful.
  8.  
  9.   The Following is an excerpt from the Borland C++ 3.0 Library Reference
  10.   discribing the syntax and use of this statment. The same apply's to this
  11.   version. :
  12.  
  13.  
  14. StrTok  [ this part (Name)  is modified for pasal use ]
  15. _____________________________________________________________________________
  16.  
  17.        Function  Searches one string for tokens, which are separated by
  18.        --------  Delimeter defined in a second string.
  19.  
  20.          Syntax  uses STRPARSE;    [ this part is modified for pasal use ]
  21.          ------  Function StrTok(Phrase:Pchar;Delimeter:PChar):Pchar;
  22.  
  23.    Prototype in  STRPARSE.PAS [ this part is modified for pasal use ]
  24.    ------------
  25.         Remarks  StrTok considers the string Phrase to consist of a sequence
  26.         -------  of zero or more text tokens,separated by spans of one or more
  27.                  characters from the separator string Delimeter.
  28.  
  29.                  The first call to StrTok returns a Pointer (PChar type for
  30.                   Pascal) to the first character of the first token in Phrase
  31.                  and writes a null (nil for Pascal) character into Phrase
  32.                  immediately following the returned token. Subsequent calls
  33.                  with null (nil for Pacsal) for the first argument will
  34.                  work through the string Phrase in this way, unitl no tokens
  35.                  remain.
  36.  
  37.                  The separator string,Delimeter, can be different from call
  38.                  to call.
  39.  
  40.    Return Value  StrTok returns a pointer (PChar) to the token found in Phrase
  41.    ------------  A null (Nil fo Pascal) is returned when there are no more
  42.                  tokens.
  43.  
  44.         Example
  45.         -------
  46.  
  47.                Function GetPrinterDC:HDC;
  48.                 var
  49.                  szPrinter : array [0..80] of Char;
  50.                  szDevice,
  51.                  szDriver,
  52.                  szOutput  : PChar;
  53.  
  54.                 begin
  55.  
  56.                  GetProfileString('windows','device',',,,',szPrinter,80);
  57.  
  58.                  szDevice := StrTok(szPrinter,',');
  59.                  szDriver := StrTok(Nil      ,',');
  60.                  szOutput := StrTok(Nil      ,',');
  61.  
  62.  
  63.                  if ((szDevice<>Nil) and (szDriver<>Nil) and (szOutput<>Nil)) Then
  64.                      GetPrinterDC := CreateDC (szDriver,szDevice,szOutput,nil)
  65.                  else
  66.                      GetPrinterDC := 0;
  67.  
  68.                 end;
  69.  
  70.  
  71.    That is the offical Borland Explanation of how te funtion works. I Hope
  72.    that in my efforts I have faithfully reproduced it's functionality. I
  73.    hope that some of you will find this function useful, and I now declare
  74.    this function to be Public domain, use as you whish.
  75.  
  76.         I Would however apprieciate feed back on it,(you know bugs ECT.)
  77.    at 76356,3601.
  78.  
  79.                  This function was written by:
  80.  
  81.                                John Cooper
  82.                          Tower Software & Systems
  83.  
  84.                        and is donated to Public Domain.
  85.  
  86.   NOTE : This Function Physically alters your original string, so if you
  87.   ----   Want or need to use your string somewhere else in your program
  88.          send StrTok only a COPY of your original string.
  89.  
  90.                                                          Thanks.
  91.  
  92. }
  93.  
  94. interface
  95.  
  96. uses Strings;
  97.  
  98. Function StrTok(Phrase:Pchar;Delimeter:PChar):Pchar;
  99.  
  100. implementation
  101.  
  102. var
  103.     TokenPointer,
  104.     WorkPointer : PChar;
  105.  
  106.  { String Manipulation Routines }
  107.  
  108.  
  109. Function StrTok(Phrase:Pchar;Delimeter:PChar):Pchar;
  110.  var
  111.   NullPointer : Pchar;
  112.  begin
  113.  
  114.   if (Phrase<>Nil) then
  115.    begin
  116.     WorkPointer := Phrase;
  117.    end
  118.   else
  119.    begin
  120.     WorkPointer := TokenPointer;
  121.    end;
  122.  
  123.   NullPointer := StrPos(WorkPointer,Delimeter);
  124.  
  125.  
  126.   if (NullPointer<> Nil) then
  127.    NullPointer^ := Chr(0);
  128.  
  129.   TokenPointer  := NullPointer + 1;
  130.  
  131.  
  132.   StrTok := WorkPointer
  133.  
  134.  end;
  135.  
  136. end.